package com.ssh.admin.action;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.Activity;
import com.ssh.entity.ActivitySuperMarket;
import com.ssh.entity.SuperMarket;
import com.ssh.manager.ActivityManager;
import com.ssh.manager.ActivityMarketManager;
import com.ssh.manager.SuperMarketManager;

@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/activity/activityMarket.jsp"),
@Result(name="findList", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.createDate,page\\.pageData\\[\\d+\\]\\.planHaibao,page\\.pageData\\[\\d+\\]\\.planDuitou," +
"page\\.pageData\\[\\d+\\]\\.planToutao,page\\.pageData\\[\\d+\\]\\.planDuanjia,page\\.pageData\\[\\d+\\]\\.planChenlie,page\\.pageData\\[\\d+\\]\\.haibao," +
"page\\.pageData\\[\\d+\\]\\.duitou,page\\.pageData\\[\\d+\\]\\.toutao,page\\.pageData\\[\\d+\\]\\.duanjia,page\\.pageData\\[\\d+\\]\\.chenlie," +
"page\\.pageData\\[\\d+\\]\\.activity\\.id,page\\.pageData\\[\\d+\\]\\.activity\\.subject,page\\.pageData\\[\\d+\\]\\.activity\\.content,page\\.pageData\\[\\d+\\]\\.activity\\.brand\\.name," +
"page\\.pageData\\[\\d+\\]\\.activity\\.brand\\.id,page\\.pageData\\[\\d+\\]\\.superMarket\\.id,page\\.pageData\\[\\d+\\]\\.superMarket\\.name,page\\.pageData\\[\\d+\\]\\.superMarket\\.company\\.name"}),
@Result(name="update", type="json", params={"root","jr","excludeproperties","jr\\.code"}),
@Result(name="delete", type="json", params={"root","page","excludeproperties","page\\.curPageNum"})
})
public class ActivityMarketAction extends ActionSupport{
	@Resource(name="superMarketManagerImpl")
	private SuperMarketManager superMarketManager;
	@Resource(name="activityManagerImpl")
	private ActivityManager activityManager;
	@Resource(name="activityMarketManagerImpl")
	private ActivityMarketManager activityMarketManager;
	
	private Page page = new Page();
	private JsonResult jr=new JsonResult();
	private String isDesc;
	private Activity activity;
	private SuperMarket superMarket;
	private ActivitySuperMarket activitySuperMarket;
	private String activityStr;
	private String marketStr;
	private String ids;
	
	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public ActivitySuperMarket getActivitySuperMarket() {
		return activitySuperMarket;
	}

	public void setActivitySuperMarket(ActivitySuperMarket activitySuperMarket) {
		this.activitySuperMarket = activitySuperMarket;
	}

	public String getActivityStr() {
		return activityStr;
	}

	public void setActivityStr(String activityStr) {
		this.activityStr = activityStr;
	}

	public String getMarketStr() {
		return marketStr;
	}

	public void setMarketStr(String marketStr) {
		this.marketStr = marketStr;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public JsonResult getJr() {
		return jr;
	}

	public void setJr(JsonResult jr) {
		this.jr = jr;
	}

	public String getIsDesc() {
		return isDesc;
	}

	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}

	public Activity getActivity() {
		return activity;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public SuperMarket getSuperMarket() {
		return superMarket;
	}

	public void setSuperMarket(SuperMarket superMarket) {
		this.superMarket = superMarket;
	}

	public String goJsp(){
		return "success";
	}
	
	public String add(){
		//String[] a = (String[]) ActionContext.getContext().getParameters().get("activityCheckBox");
		//String[] m = (String[]) ActionContext.getContext().getParameters().get("marketCheckBox");
		
		String[] a = activityStr.split(",");
		String[] m = marketStr.split(",");
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < m.length; j++) {
				activity = activityManager.getById(new Long(a[i]));
				superMarket = superMarketManager.getSuperMarketById(new Long(m[j]));
				ActivitySuperMarket as = new ActivitySuperMarket();
				as.setActivity(activity);
				as.setSuperMarket(superMarket);
				as.setPlanChenlie(activitySuperMarket.getPlanChenlie());
				as.setPlanDuanjia(activitySuperMarket.getPlanDuanjia());
				as.setPlanDuitou(activitySuperMarket.getPlanDuitou());
				as.setPlanHaibao(activitySuperMarket.getPlanHaibao());
				as.setPlanToutao(activitySuperMarket.getPlanToutao());
				as.setHaibao(0);
				as.setDuitou(0);
				as.setToutao(0);
				as.setDuanjia(0);
				as.setChenlie(0);
				as.setCreateDate(new Timestamp(System.currentTimeMillis()));
				activityMarketManager.saveUpdate(as);
			}
		}
		return "success";
	}
	
	public String delete(){
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					activityMarketManager.delete(new Long(d[i]));
				}
			}else{
				activityMarketManager.delete(activitySuperMarket.getId());
			}
			int _p = (int) page.getCurPageNum();
			page.setCurPageNum(1);
			Map conditionMap = new HashMap();
			conditionMap.put("activity", activity.getContent());
			conditionMap.put("market", superMarket.getId());
			activityMarketManager.findList(conditionMap, page, isDesc);
			if(_p<=page.getTotalPageNum()){
				page.setCurPageNum(_p);
			}else{
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			e.printStackTrace();
		}
		return "delete";
	}
	
	public String update(){
		
		return "update";
	}
	
	public String findList(){
		Map conditionMap = new HashMap();
		conditionMap.put("activity", activity.getContent());
		conditionMap.put("market", superMarket.getId());
		activityMarketManager.findList(conditionMap, page, isDesc);
		return "findList";
	}
}
